#include <purewell/purewell.hpp>
#include <purewell/filesystemstatus.hpp>
#include <purewell/filestatus.hpp>
using namespace purewell;

#include <iostream>
using namespace std;

#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

char*
getTimestring(time_t now)
{
	const struct tm& tm(*localtime(&now));
	static char buf[1024];
	sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d",
		tm.tm_year+1900,
		tm.tm_mon+1,
		tm.tm_mday,
		tm.tm_hour,
		tm.tm_min,
		tm.tm_sec);
	return buf;
}

void
statFile(const char* path)
{
	cout << "file: " << path << endl;
	PWFileStatus fs;
	if ( !fs.getStatus(path) )
	{
		cout << strerror(errno) << endl;
		return;
	}

	if ( fs.isSocket() ) cout << "Socket" << endl;
	if ( fs.isRegularFile() ) cout << "Regular file" << endl;
	if ( fs.isDirectory() ) cout << "Directory" << endl;
	if ( fs.isCharacterDevice() ) cout << "Character device" << endl;
	if ( fs.isBlockDevice() ) cout << "Block device" << endl;
	if ( fs.isFIFO() ) cout << "FIFO" << endl;
	if ( fs.isSymbolicLink() ) cout << "Symbolic link" << endl;
	cout << "Access time: " << getTimestring(fs.getAccessTime()) << endl;
	cout << "Modification time: " << getTimestring(fs.getModificationTime()) << endl;
	cout << "Change time: " << getTimestring(fs.getChangeTime()) << endl;

	return;
}

void
statFS(const char* path)
{
	cout << "file system: " << path << endl;
	PWFileSystemStatus fs;
	if ( !fs.getStatus(path) )
	{
		cout << strerror(errno) << endl;
		return;
	}

	cout << "Block size: " << fs.getBlockSize() << endl;
	cout << "Fragment size: " << fs.getFragmentSize() << endl;
	cout << "Blocks: " << fs.getBlocks() << endl;
	cout << "Free blocks: " << fs.getFreeBlocks() << endl;
	cout << "Available blocks: " << fs.getAvailableBlocks() << endl;
	cout << "INodes: " << fs.getINodes() << endl;
	cout << "Free inodes: " << fs.getFreeINodes() << endl;
	cout << "Available inodes: " << fs.getAvailableINodes() << endl;
	cout << "ID: " << fs.getID() << endl;
	cout << "Flags: " << fs.getFlags() << endl;
	cout << "Max file name size: " << fs.getMaxFileNameSize() << endl;
	cout << "Total size: " << fs.getBlockSize() * fs.getBlocks() /1024.0/1024.0/1024.0 << "GB" << endl;
	cout << "Free size: " << fs.getBlockSize() * fs.getAvailableBlocks() /1024.0/1024.0/1024.0 << "GB" << endl;
}

int
main(int argc, char* argv[])
{
	if ( argc < 2 )
	{
		cout << argv[0] << " [file]" << endl;
		return 0;
	}

	statFile(argv[1]);
	statFS(argv[1]);

	return 0;
}

